<?php

namespace app\api\controller;

use app\common\model\ApplicationsModel;
use think\App;
use app\common\model\EquipmentsModel;
use app\common\model\AccountModel;
use think\Db;

/**  TX
 *   企业API
 *   Date: 2020/8/13
 * Class Company
 * @package app\api\controller
 */
class Equipments extends Base
{
    protected $equipmentsModel;
    protected $accountModel;
    protected $applicationsModel;

    // 初始化构造方法
    public function __construct(App $app = null)
    {
        parent::__construct($app);
        $this->equipmentsModel = new EquipmentsModel();  // 产品模型
        $this->accountModel = new AccountModel();  // 账户模型
        $this->applicationsModel = new ApplicationsModel(); // 账户表模型

    }

    // 产品列表
    public function getLists()
    {
        // 接收参数
        $page = $this->parm['page'] ?? 1;
        $pageSize = $this->parm['pageSize'] ?? 10;

        $where = [];
        if (!empty($this->parm['equipments_sn'] ?? '')) {
            $where['equipments_sn'] = $this->parm['equipments_sn'];
        }
        if (!empty($this->parm['company_id'] ?? '')) {
            //$account_info=$this->accountModel->getCompanyInfo($this->parm['company_id']);
            //$where['company_no']=$account_info['company_no'];
            $where['company_id'] = $this->parm['company_id'];
        }
        $api_host = $this->applicationsModel->getApiHost($this->parm['app_sn']);
        $api_url = $api_host . $this->api_url['getEquipmentsLists'];
        $result = http_post($api_url, json_encode($where));
        if ($result['code'] == 1) {
            returnJson(1, '暂无数据');
        }

        returnJson(0, '获取成功', $result['data'], $result['count'], $result['info']);
    }


    //设置服务费
    public function sets()
    {
        //接收参数
        $param = $this->request->param();
        if (empty($this->parm['app_sn'])) {
            returnJson(1, '缺少app_sn');
        }
        if (empty($param['equipments_sn'])) {
            returnJson(1, '缺少产品编号');
        }
        if (empty($param['company_id'])) {
            returnJson(1, '缺少参数company_id');
        }
        if (empty($param['service_pay_role'])) {
            returnJson(1, '请选择服务支付角色');
        }
        if (empty($param['new_install_service_money'])) {
            returnJson(1, '请输新装服务费');
        }
        if (empty($param['renew_service_money'])) {
            returnJson(1, '请输入续约服务费');
        }

        $map['equipments_sn'] = $param['equipments_sn'];
        //查询产品信息
        $api_host = $this->applicationsModel->getApiHost($this->parm['app_sn']);
        $api_url = $api_host. $this->api_url['getEquipmentsInfo'];
        $result = http_post($api_url, json_encode($map));
        if ($result['code'] == 1) {
            returnJson(1, '产品信息错误');
        }

        //获取企业信息
        $company_info = $this->accountModel->getCompanyInfo($param['company_id']);
        if (empty($company_info)) {
            returnJson(1, '企业信息错误');
        }

        //查询信息
        $equipments_info = $this->equipmentsModel->getOne(['equipments_sn' => $param['equipments_sn']], 'id');
        Db::startTrans();
        try {
            if (!empty($equipments_info)) {
                $edit_data = [
                    'new_install_service_money' => formatMoney($param['new_install_service_money'], 1),
                    'renew_service_money' => formatMoney($param['renew_service_money'], 1),
                    'service_pay_role' => $param['service_pay_role'],
                    'remarks' => $param['remarks'],
                ];

                $this->equipmentsModel->edit(['id' => $equipments_info['id']], $edit_data);
            } else {
                $add_data = [
                    'equipments_sn' => $result['data']['equipments_sn'],
                    'app_sn' => $company_info['app_sn'],
                    'company_no' => $company_info['company_no'],
                    'new_install_service_money' => formatMoney($param['new_install_service_money'], 1),
                    'renew_service_money' => formatMoney($param['renew_service_money'], 1),
                    'service_pay_role' => $param['service_pay_role'],
                    'create_time' => time(),
                    'remarks' => $param['remarks'],
                ];
                $this->equipmentsModel->add($add_data);

            }

            Db::commit();
            returnJson(0, '操作成功');
        } catch (\Exception $e) {
            // 事务回滚
            Db::rollback();
            returnJson(1, "编辑失败" . $e->getMessage());
        }

    }


}